package com.example.myanimator.six;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/19
 * @describe PathMeasure 动画画圆
 */
public class DrawCircleAnimatorView extends View {

    private PathMeasure mPathMeasure;
    private Path mPath;
    private Paint mPaint;
    private float startPoint = 0;
    private float mProgress;


    public DrawCircleAnimatorView(Context context) {
        super(context);
    }

    public DrawCircleAnimatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawCircleAnimatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.YELLOW);

        mPath = new Path();
        mPath.addCircle(100, 100, 50, Path.Direction.CCW);
        mPathMeasure = new PathMeasure(mPath, false);

        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
//                startPoint = endPoint;
                mProgress = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.setDuration(3000);
        animator.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        drawCircle1(canvas);
        drawCircle2(canvas);


    }
    private void drawCircle1(Canvas canvas) {
        canvas.translate(200, 300);
        canvas.save();
        float endLength = (float)(mPathMeasure.getLength()*mProgress);
        mPath.reset();
        Log.d("tgw-PathMeasure", "mPathMeasure-onDraw: " + mPathMeasure.getLength() +"圆绘制 动画进度"+mProgress);
        mPathMeasure.getSegment(0,endLength, mPath, true);
        canvas.drawPath(mPath, mPaint);
        canvas.restore();

    }
    private void drawCircle2(Canvas canvas) {
        canvas.translate(400, 300);
        float endLength = (float)(mPathMeasure.getLength()*mProgress);
//        startPoint =(float) ((0.9*mPathMeasure.getLength()));
        startPoint= (float) (endLength - ((0.5 - Math . abs(mProgress - 0.5)) *
                mPathMeasure.getLength())) ;

        mPath.reset();
        Log.d("tgw-PathMeasure", "mPathMeasure-startPoint: " +startPoint +"圆绘制 动画进度"+mProgress);
        mPathMeasure.getSegment(startPoint,endLength, mPath, true);
        canvas.drawPath(mPath, mPaint);
    }
}
